package cc.ipost.ipost;

import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cc.ipost.ipost.data.RequestData;

import com.github.droidfu.widgets.WebImageView;

public class HistoryAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private HistoryActivity context;
	private List<HashMap<String, Object>> hList;

	public HistoryAdapter(HistoryActivity c, List<HashMap<String, Object>> hlist) {
		this.context = c;
		this.hList = hlist;
		this.mInflater = LayoutInflater.from(context);

	}

	public int getCount() {
		return this.hList.size();
	}

	public Object getItem(int arg0) {
		return hList.get(arg0);
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.history_item, null);
			vh = new ViewHolder();
			vh.date = (TextView) convertView
					.findViewById(R.id.card_create_date);
			vh.time = (TextView) convertView
					.findViewById(R.id.card_create_time);
			vh.receiver = (TextView) convertView
					.findViewById(R.id.card_receiver);
			vh.text = (TextView) convertView.findViewById(R.id.card_text);
			vh.status = (TextView) convertView.findViewById(R.id.card_status);
			vh.cover = (WebImageView) convertView.findViewById(R.id.card_cover);
			vh.delbtn = (RelativeLayout) convertView
					.findViewById(R.id.card_delbtn);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		HashMap<String, Object> h = hList.get(position);
		vh.date.setText(h.get("date").toString());
		vh.time.setText(h.get("time").toString());
		vh.receiver.setText(h.get("receiver").toString().toUpperCase());
		vh.text.setText(h.get("text").toString());
		vh.status.setText(h.get("status").toString().toUpperCase());
		final String url = MySettings.HOST + h.get("cover").toString();
		// url = URLEncoder.encode(url);
		System.out.println(url);
		vh.cover.setImageUrl(url);
		vh.cover.loadImage();
		final String coverBigUrl = MySettings.HOST
				+ h.get("cover_big").toString();
		vh.cover.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				View coverPreview = mInflater.inflate(
						R.layout.history_cover_preview, null);
				WebImageView c = (WebImageView) coverPreview
						.findViewById(R.id.card_cover_preview);
				c.setImageUrl(coverBigUrl);
				c.loadImage();
				View closeBtn = coverPreview
						.findViewById(R.id.card_cover_preview_close);
				final PopupWindow cp = new PopupWindow(coverPreview, 660, 483, true);
				final View rv = context.getWindow().getDecorView()
						.findViewById(android.R.id.content);
				cp.setBackgroundDrawable(new BitmapDrawable());
				cp.showAtLocation(rv, Gravity.CENTER, 0, 0);
				closeBtn.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						cp.dismiss();
					}
				});
			}
		});
		vh.delbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				new AlertDialog.Builder(context).setMessage(context.getString(R.string.delete_confirm))
						.setPositiveButton(context.getString(R.string.ok), new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								
								MyApplication ma = (MyApplication) context.getApplication();
								RequestData rd = new RequestData(String.format(MySettings.UCARD,ma.user.ID), RequestData.DELETE);
								WebServiceTask wst = new WebServiceTask(rd, context.delHandler);
								wst.execute("");
							}
						})
						.setNegativeButton(context.getString(R.string.cancel), null)
						.setCancelable(true)
						.show();
			}
		});
		return convertView;
	}

	static class ViewHolder {
		TextView date;
		TextView time;
		TextView receiver;
		TextView text;
		TextView status;
		WebImageView cover;
		RelativeLayout delbtn;
	};

}
